package ch.bfh.swos.learnaid.rest.controller;

import java.util.Collection;

import javax.inject.Inject;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import ch.bfh.swos.learnaid.service.TagService;
import ch.bfh.swos.learnaid.service.dto.TagDTO;

@Controller
@RequestMapping("/tags")
public class TagController {

    @Inject
    private TagService tagService;

    /**
     * Create
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public TagDTO createTag(@RequestBody TagDTO tag) {
        TagDTO createdtag= tagService.create(tag);
        System.out.println("tags created with id = " + createdtag.getId());
        return createdtag;
    }

    /**
     * ReadAll
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Collection<TagDTO> getTags() {
        System.out.println("Collection of tags requested");
        return tagService.list();
    }

    /**
     * Read
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public TagDTO getTag(@PathVariable long id) {
        System.out.println("tags requested with id = " + id);
        return tagService.read(id);
    }

    /**
     * Update
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    @ResponseBody
    public TagDTO updateTag(@RequestBody TagDTO tag, @PathVariable long id) {
        TagDTO updatedtag = tagService.update(tag);
        System.out.println("tag updated with id = " + updatedtag.getId());
        return updatedtag;
    }

    /**
     * Delete
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public void deleteTag(@PathVariable long id) {
        TagDTO tag = tagService.read(id);
        tagService.delete(tag);
        System.out.println("Delete tag with id = " + id);
    }
}
